﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Employee
{
    public partial class FrmAccountDetail : DevComponents.DotNetBar.Office2007Form
    {
        public AccountDTO Account { get; set; }
        public ActionName Action { get; set; }
        AccountBUS AccBUS;

        public FrmAccountDetail()
        {
            InitializeComponent();
            cmbEmployee.DisplayMember = EmployeeColumn.EmployeeName.ToString();
            cmbEmployee.ValueMember = EmployeeColumn.EmployeeID.ToString();
            btnUpdate.Location = btnAdd.Location;
            AccBUS = new AccountBUS();
        }

        private void FrmAccountDetail_Load(object sender, EventArgs e)
        {
            if (Action == ActionName.Insert)
            {
                txtUserName.Focus();
                Account = new AccountDTO();
                EmployeeBUS EmpBUS = new EmployeeBUS();
                List<EmployeeDTO> listEmployee = EmpBUS.GetEmployeeListNotHasAccount();
                cmbEmployee.DataSource = listEmployee;
                ProcessButton();
            }
            else if (Action == ActionName.Update && Account != null)
            {
                txtUserName.Text = Account.UserName;
                txtPassWord.Text = Account.Password;
                txtUserName.ReadOnly = true;

                List<EmployeeDTO> listEmp = new List<EmployeeDTO>();
                listEmp.Add( new EmployeeDTO { EmployeeName = Account.EmployeeName, EmployeeID = Account.EmployeeID });
                cmbEmployee.DataSource = listEmp;
                cmbEmployee.SelectedIndex = 0;
                cmbEmployee.Enabled = false;
            }
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnAdd.Visible = true;
                btnAdd.Enabled = true;
                btnUpdate.Visible = false;
                cmbEmployee.Enabled = true;
                txtUserName.Enabled = true;
            }
            else if (Action == ActionName.Update)
            {
                btnAdd.Visible = false;
                btnUpdate.Visible = true;
                cmbEmployee.Enabled = false;
                txtUserName.Enabled = false;
            }
        }

        private bool CheckDataInput()
        {
            if (cmbEmployee.Items.Count <= 0)
            {
                Global.SetMessage(lblMessage, "Toàn bộ nhân viên đã có tài khoản!", false);
                return false;
            }

            if(string.IsNullOrEmpty(txtUserName.Text))
            {
                Global.SetMessage(lblMessage,"Tên đăng nhập không được trống!",false);
                txtUserName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtPassWord.Text) || string.IsNullOrEmpty(txtRePassword.Text))
            {
                Global.SetMessage(lblMessage, "Mật khẩu không được để trống!", false);
                txtPassWord.Focus();
                return false;
            }

            if (!txtPassWord.Text.Equals(txtRePassword.Text))
            {
                Global.SetMessage(lblMessage, "Mật khẩu xác nhận không đúng!", false);
                return false;
            }

            return true;
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Account = new AccountDTO();

            EmployeeBUS EmpBUS = new EmployeeBUS();
            List<EmployeeDTO> listEmployee = EmpBUS.GetEmployeeListNotHasAccount();
            //cmbEmployee.DataSource.c
            cmbEmployee.ResetText();
            cmbEmployee.DataSource = listEmployee;

            txtPassWord.Clear();
            txtRePassword.Clear();
            txtUserName.Clear();
            txtUserName.ReadOnly = false;
            Action = ActionName.Insert;
            ProcessButton();
            lblMessage.Text = string.Empty;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            Account.EmployeeID = Global.intParse( cmbEmployee.SelectedValue);
            Account.UserName = txtUserName.Text;
            Account.Password = txtPassWord.Text;

            int result = AccBUS.InsertAccount(Account);
            if (result > 0)
            {
                Global.SetMessage(lblMessage, "Thêm tài khoản thành công!", true);

                Action = ActionName.Update;
                ProcessButton();

                Employee.FrmAccountList frm = (Employee.FrmAccountList)Application.OpenForms["FrmAccountList"];
                if (frm != null)
                {
                    Account = AccBUS.GetAccountByID(Account.EmployeeID);
                    frm.AccountAfterInsert(Account);
                }
            }
            else if (result == -1)
            {
                Global.SetMessage(lblMessage, "Tên tài khoản đã tồn tại!", false);
                return;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            Account.Password = txtPassWord.Text;

            if (AccBUS.UpdateAccount(Account))
            {
                Global.SetMessage(lblMessage, "Thay đổi mật khẩu thành công!", true);
            }
            else
            {
                Global.SetMessage(lblMessage, "Có lỗi trong quá trình thực hiện!", false);
            }
        }
    }
}
